
public class isMatch 
{
	public boolean ismatch3(String s, String p)
	{
int s1 = 0, s2 = -1, p1 = 0, p2 = -1;
        
        while(s1<s.length())
        {
            if(p1<p.length() && (s.charAt(s1) == p.charAt(p1) || p.charAt(p1) == '?'))
            {
                s1++;
                p1++;
            }
            else if(p1<p.length() && p.charAt(p1) == '*')
            {
                p1++;
                p2 = p1;
                s2 = s1;
            }
            else if(p2>=0)
            {
                s1 = s2 + 1;
                s2++;
                p1 = p2;
            }
            else
            {
                return false;
            }
        }
        
        while(p1<p.length() && p.charAt(p1) == '*') p1++;
        
        return (p1 == p.length());
	}
	
	public boolean ismatch(String str, String pattern)
	{
        int s = 0, p = 0, match = 0, starIdx = -1;            
        while (s < str.length()){
            // advancing both pointers
            if (p < pattern.length()  && (pattern.charAt(p) == '?' || str.charAt(s) == pattern.charAt(p))){
                s++;
                p++;
            }
            // * found, only advancing pattern pointer
            else if (p < pattern.length() && pattern.charAt(p) == '*'){
                starIdx = p;
                match = s;
                p++;
            }
           // last pattern pointer was *, advancing string pointer
            else if (starIdx != -1){
                p = starIdx + 1;
                match++;
                s = match;
            }
           //current pattern pointer is not star, last patter pointer was not *
          //characters do not match
            else return false;
        }

        //check for remaining characters in pattern
        while (p < pattern.length() && pattern.charAt(p) == '*')
            p++;

        return p == pattern.length();
	}
	
    public boolean ismatch2(String s, String p) {
        int s1 = 0;
        int p1 = 0;
        
        if(s.length() == 0)
        {
            while(p1<p.length() && p.charAt(p1) == '*'){p1++;}
            if(p1 == p.length()) return true;
            else return false;
        }
        if(p.length() == 0) return false;
        
        
        switch(check(s.charAt(0), p.charAt(0)))
        {
            case 0:
                return false;
            case 1:
                return ismatch2(s.substring(1), p.substring(1));
        }
        
        // leave the case: x...., *....
        
        if(p.length() == 1) return true; // remove case: x..., *
        
        while((p1 < p.length()) && p.charAt(p1) == '*') {
            p1++;
        }
        
        
        boolean restMatch = ismatch2(s.substring(s1), p.substring(p1));
        
        while(! restMatch)
        {
            s1++;
            if(s1 >= s.length()) return false;
            restMatch = ismatch2(s.substring(s1), p.substring(p1));
        }
        
        return true;
    }
    
    public int check(char a, char b)
    {
        if(a == '?' || b == '?') return 1;
        if(a == '*' || b == '*') return 2;
        if(a == b) return 1;
        return 0;
    }
}
